package JDBC1;

import org.junit.Test;

import java.sql.*;

public class JdbcStudy {

    private String url="jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimezone=UTC";
    private String user="root";
    private String password="123456";

    public JdbcStudy() {
    }

    public JdbcStudy(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    /**
     * 链接数据库的方法
     * @return
     */
    public  Connection conn(){
        Connection conn=null;
        try {
            //1.注册操作数据的驱动、反射
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.使用api链接数据库
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("conn"+conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 实现数据库增删改的sql语句的执行
     * @param conn 数据库链接对象
     * @param sql  执行的sql语句
     * @return  int 执行的结果
     */
    public static int stateDbj(Connection conn,String sql){
        int i=0;
        try {
            Statement statement=conn.createStatement();
            i=statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Test
    public void findAllData(String sql) throws Exception {
        Connection conn = conn();
        //3.定义查询语句3
        //  String sql = "select * from emp";
        //4.获取查询对象
        Statement statement = conn.createStatement();
        System.out.println("statement = " + statement);
        //5.使用查询对象执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet = " + resultSet);
        //6.解析数据
        while (resultSet.next()) {
            int id = resultSet.getInt("EMPNO");
            String username = resultSet.getString("ENAME");
            System.out.println(id + " | " + username );
        }
        //7.释放资源
        statement.close();
        conn.close();

    }

    /**
     * 实现数据入库操作
     * @throws Exception
     */
    @Test
    public void insertData(String sql) throws Exception {
        Connection conn=conn();
        int i = JdbcStudy.stateDbj(conn, sql);
          System.out.println("i="+i);
    }

    /**
     * 删除数据
     * @throws Exception
     */
    @Test
    public void deleteDate(String sql) throws Exception {
        Connection conn=conn();
        int i = JdbcStudy.stateDbj(conn, sql);
        System.out.println("i="+i);
    }
    /**
     * 修改数据
     * @throws Exception
     */
    @Test
    public void updateDate(String sql) throws Exception {
        Connection conn=conn();
        int i = JdbcStudy.stateDbj(conn, sql);
        System.out.println("i="+i);
    }

    public boolean isAdmin(String admin,String pwd) throws Exception{
        Connection conn=conn();
        Statement statement = conn.createStatement();
        String sql="select * from manger where admin='"+admin+"' and password='"+pwd+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        return  resultSet.next();
    }
}
